<template>
  <div class="categories-page">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="isDialogShow('add')"
        >添加分类</el-button
      >
      <tree-table
        :data="list"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <template #isok="{ row }">
          <i
            class="el-icon-success"
            v-if="row.cat_deleted === false"
            style="color: lightgreen"
          >
          </i>
          <i class="el-icon-error" v-else style="color: red"> </i>
        </template>
        <template #order="{ row }">
          <el-tag v-if="row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template #ipt="{ row }">
          <el-row width="250px" type="flex">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="isDialogShow('edit', row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delFn(row)"
              >删除</el-button
            >
          </el-row>
        </template>
      </tree-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagenum"
          :page-sizes="[2, 5, 7, 10]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogFormVisible"
      @close="closeFn"
    >
      <!--      <el-form v-if="dialogType === 'edit'" :rules="rules">-->
      <!--        <el-form-item label="分类名称" label-width="80px">-->
      <!--          <el-input></el-input>-->
      <!--        </el-form-item>-->
      <!--      </el-form>-->
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="分类名称" prop="name" label-width="80px">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          v-if="dialogType === 'add'"
          label="父级分类"
          prop="region"
          label-width="80px"
        >
          <el-cascader
            v-model="selectedKeys"
            :options="options"
            :props="cascaderProps"
            clearable
            expandTrigger="hover"
            @change="selectChange"
            style="width: 100%"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCategoriesApi,
  delGoodClassificationApi,
  editCategoriesApi,
  getClassificationApi,
} from "@/api/e-commerce";
import { Message } from "element-ui";

export default {
  name: "CategoriesIndex",
  data() {
    return {
      list: [],
      type: 1,
      pagenum: 1,
      pagesize: 5,
      total: 0,
      columns: [
        { label: "分类名称", prop: "cat_name" },
        { label: "是否有效", type: "template", template: "isok" },
        {
          label: "排序",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "ipt",
        },
      ],
      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
      },
      rules: { name: [{ required: true, message: "请输入分类名称" }] },
      options: [],
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true,
      },
      selectedKeys: [],
      dialogType: "",
      dialogTitle: "",
      userId: "",
      addInfo: {
        cat_pid: "",
        cat_name: "",
        cat_level: "",
      },
    };
  },
  created() {
    this.getClassification();
  },
  methods: {
    async submit() {
      try {
        await this.$refs.form.validate();
        this.dialogFormVisible = false;
        if (this.dialogType === "edit") {
          console.log(this.userId);
          await editCategoriesApi(this.userId, {
            cat_name: this.form.name,
          });
          Message.success("编辑成功");
          this.getClassification();
        } else {
          await addCategoriesApi(this.addInfo);
          Message.success("添加成功");
          this.getClassification();
        }
      } catch (err) {
        console.log(err);
      }
    },
    closeFn() {
      this.form = {};
      this.$refs.form.resetFields();
    },
    selectChange() {
      console.log(this.selectedKeys);
      this.addInfo.cat_level = this.selectedKeys.length;
      this.addInfo.cat_pid = this.selectedKeys.at(-1);
      this.addInfo.cat_name = this.form.name;
    },
    async getSelectionList() {
      const res = await getClassificationApi({
        type: 2,
      });
      console.log(res);
      this.options = res.data.data;
    },
    isDialogShow(type, row) {
      console.log(row);
      this.dialogFormVisible = true;
      this.dialogType = type;
      this.getSelectionList();
      if (type === "add") {
        this.dialogTitle = "添加分类";
      } else {
        this.dialogTitle = "编辑";
        this.form.name = row.cat_name;
        this.userId = row.cat_id;
      }
    },
    async delFn(row) {
      // console.log(row);
      try {
        await this.$confirm("此操作将删除该分类, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        await delGoodClassificationApi(row.cat_id);
        await Message.success("删除成功");
        this.getClassification();
      } catch (err) {
        Message.warning("已取消删除");
      }
    },
    async getClassification() {
      const res = await getClassificationApi({
        pagenum: this.pagenum,
        pagesize: this.pagesize,
      });
      // console.log(res);
      this.list = res.data.data.result;
      this.total = res.data.data.total;
    },
    handleSizeChange(value) {
      this.pagesize = value;
      this.getClassification();
    },
    handleCurrentChange(value) {
      this.pagenum = value;
      this.getClassification();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.zk-table {
  margin: 15px auto;
}
</style>
